<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PostController
 *
 * @author tuancasi
 */
class PostController extends Controller {

    /**
     * show list  
     */
    public function actionIndex() {

	$model = new Posts('search');
	$model->unsetAttributes();  // clear any default values

	if (isset($_GET['Posts'])) {
	    $model->attributes = $_GET['Posts'];
	}

	$this->render('index', array(
	    'model' => $model
	));
    }

    /*     * ***********************************************************
     * add post
     */

    public function actionCreate() {

	$modelPost = new Posts();

	$success = 0; //alert that posting is successful
	$error = ''; //alert that posting is fail		
	//if user press post
	if (isset($_POST['submit'])) {
	    $modelPost->attributes = $_POST['Posts'];
	    $modelPost->user_id = user()->id;
	    if ($modelPost->validate()) {
		$aNamePhoto = array();
		if (isset($_FILES['photo']['name']) && $_FILES['photo']['name'][0]) {
		    //if user upload photo				
		    $aNamePhoto = array();
		    $audio = '';
		    $video = array(
			'upload' => '',
			'iframe' => ''
		    );
		    for ($i = 0; $i < count($_FILES['photo']['name']); $i++) {
			if ($_FILES['photo']['name'][$i]) {
			    $fileSize = $_FILES['photo']['size'][$i];
			    $aImg = uploadImg(UPLOAD_PATH . 'photo/', $_FILES['photo']['tmp_name'][$i], $_FILES['photo']['name'][$i]);
			    if (isset($aImg['success'])) {
				$aNamePhoto[] = $aImg['name'];
			    }
			}
		    }
		}

		//upload audio
		if ((isset($_FILES['audio']['name']) && $_FILES['audio']['name'])) {
		    $fileSize = $_FILES['audio']['size'];
		    $aAudio = uploadAudio(UPLOAD_PATH . 'audio/', $_FILES['audio']['tmp_name'], $_FILES['audio']['name']);
		    if (isset($aAudio['success'])) {
			$audio = $aAudio['name'];
		    }
		}

		//upload video
		if ((isset($_FILES['video']['name']) && $_FILES['video']['name'])) {
		    //||(isset($_POST['iframe']) && $_POST['iframe'])) {		  
		    $fileSize = $_FILES['video']['size'];
		    $aVideo = uploadVideo(UPLOAD_PATH . 'video/', $_FILES['video']['tmp_name'], $_FILES['video']['name']);
		    if (isset($aVideo['success'])) {
			$video['upload'] = $aVideo['name'];
		    }
//		    if ($_POST['iframe']) {
//			$video['iframe'] = $_POST['iframe'];
//		    }		    
		}
		if (!$error) {
		    $attachment = array(
			'photo' => $aNamePhoto,
			'audio' => $audio,
			'video' => $video
		    );
		    $modelPost->attachment = serialize($attachment);
		    $modelPost->save();
		    $this->redirect(BACKEND_SITE_URL . 'post');
		}
	    }
	}

	$this->render('create', array(
	    'modelPost' => $modelPost,
	    'success' => $success,
	    'error' => $error
	));
    }

    /*     * ***********************************************************
     * edit post
     */

    public function actionUpdate() {
	if (isset($_GET['id'])) {
	    $success = 0; //alert that posting is successful
	    $error = array(); //alert that posting is fail	

	    $modelPost = Posts::model()->findByPk($_GET['id']);

	    $attachment = unserialize($modelPost->attachment);

	    //if user update
	    if (isset($_POST['submit'])) {

		$modelPost->attributes = $_POST['Posts'];
		if ($modelPost->validate()) {
		    if (isset($_FILES['photo']['name']) && $_FILES['photo']['name'][0]) {
			for ($i = 0; $i < count($_FILES['photo']['name']); $i++) {
			    if ($_FILES['photo']['name'][$i]) {
				$aImg = uploadImg(UPLOAD_PATH . 'photo/', $_FILES['photo']['tmp_name'][$i], $_FILES['photo']['name'][$i]);
				if (isset($aImg['success'])) {
				    //push to old array photo
				    array_push($attachment['photo'], $aImg['name']);
				    $aNamePhoto[] = $aImg['name'];
				}
			    }
			}
		    }

		    //upload audio
		    if ((isset($_FILES['audio']['name']) && $_FILES['audio']['name'])) {

			$aAudio = uploadAudio(UPLOAD_PATH . 'audio/', $_FILES['audio']['tmp_name'], $_FILES['audio']['name']);
			if (isset($aAudio['success'])) {
			    $attachment['audio'] = $aAudio['name'];
			}
		    }

		    //upload video
		    if ((isset($_FILES['video']['name']) && $_FILES['video']['name'])) {
			//||(isset($_POST['iframe']) && $_POST['iframe'])) {						
			$aVideo = uploadVideo(UPLOAD_PATH . 'video/', $_FILES['video']['tmp_name'], $_FILES['video']['name']);
			if (isset($aVideo['success'])) {
			    $attachment['video']['upload'] = $aVideo['name'];
			}
//		    if ($_POST['iframe']) {
//			$video['iframe'] = $_POST['iframe'];
//		    }			
		    }
		    if (!$error) {
			$modelPost->attachment = serialize($attachment);
			$modelPost->save();
			$attachment = unserialize($modelPost->attachment);
		    }
		}
	    }

	    $this->render('update', array(
		'modelPost' => $modelPost,
		'success' => $success,
		'error' => $error,
		'attachment' => $attachment
	    ));
	}
    }

    /*     * ************************************************************************************
     * delete post
     */

    public function actionDelete() {
	if (isset($_GET['id'])) {
	    $modelPost = Posts::model()->findByPk(intval($_GET['id']))->delete();
	}
    }

    /**
     * delete image when edit post
     */
    public function actionDelete_file() {
	if (isset($_POST['fileName']) && isset($_POST['fileType']) && isset($_POST['postId'])) {
	    $model = Posts::model()->findByPk($_POST['postId']);
	    $aAttachment = unserialize($model->attachment);
	    switch ($_POST['fileType']) {
		case 'photo':
		    $key = array_search($_POST['fileName'], $aAttachment['photo']);
		    unset($aAttachment['photo'][$key]);
		    deleteFile(UPLOAD_PATH . 'photo/' . $_POST['fileName']);
		    break;
		case 'audio':
		    $aAttachment['audio'] = '';
		    deleteFile(UPLOAD_PATH . 'audio/' . $_POST['fileName']);
		    break;
		case 'video':
		    $aAttachment['video']['upload'] = '';
		    deleteFile(UPLOAD_PATH . 'video/' . $_POST['fileName']);
		    break;
	    }
	    $model->attachment = serialize($aAttachment);
	    $model->save();
	}
    }

    /*     * *********************************REPORT ABUSE ***************************** */
    /*
     * view report abuse
     */

    public function actionReportAbuse() {
	$model = new Abuse('search');
	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['Abuse'])) {
	    $model->attributes = $_GET['Abuse'];
	}
	/**
	 * get all category
	 */
	$aStAbuse = Yii::app()->db->createCommand('SELECT * FROM {{statement_abuses}}')->queryAll();
	$aStatementAbuse = array();
	foreach ($aStAbuse as $abuse) {
	    $aStatementAbuse[$abuse['text']] = $abuse['text'];
	}

	$this->render('reportabuse', array(
	    'model' => $model,
	    'aStatementAbuse' => $aStatementAbuse
	));
    }

    /*
     * delete  report and delete post or comment
     */

    public function actionDeleteReportAbuse() {
	if (isset($_GET['id']) && isset($_GET['item_id']) && isset($_GET['type'])) {
	    $model = Abuse::model()->findByPk(intval($_GET['id']))->delete();
	    if ($_GET['type'] == Abuse::POST_TYPE) {
		$modelPost = Posts::model()->findByPk($_GET['item_id']);
		if ($modelPost) {
		    $modelPost->delete();
		}
	    } elseif ($_GET['type'] == Abuse::COMMENT_TYPE) {
		$modelComment = Comment::model()->findByPk($_GET['item_id']);
		if ($modelComment) {
		    $modelComment->delete();
		}
	    }
	}
    }

    /**
     * ignore abuse
     */
    public function actionIgnore_abuse() {
	if (isset($_GET['id'])) {
	    Yii::app()->db->createCommand('Update {{abuses}} SET is_ignore=' . Abuse::IGNORE . ' WHERE abuse_id=' . $_GET['id'])->execute();

	    if (isset($_GET['type'])) {
		$this->redirect(url('post/reportabuse?type=' . $_GET['type']));
	    } else {
		$this->redirect(url('post/reportabuse'));
	    }
	}
    }

}

